<?php

namespace App\Lib;

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);

class DingDingRobot
{
    // 使用文档：https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

    private $webHookApi = 'https://oapi.dingtalk.com/robot/send?access_token=d394ad9b8084e82a35e7f9c89c305886eb6ca129da74ffed23baa168cb3ed980';

    /**
     * 充值预警
     * @param array $params 参数
     */
    public function rechargeEarlyWarning($params)
    {
        $device = '';
        if ($params['pay_type'] == 2) {
            $device = 'iOS';
        }
        // $userImage = $params['user']['images'][0] ?? '';
        $uids = $params['uids'] ?? '';

        $text = "#### 充值预警\n\n" .
            "> {$device} 充值用户：{$params['user']['nickname']}\n" .
            // "> ![screenshot]({$userImage})\n" .
            "> ###### 用户UID：{$params['user']['user_uid']}\n\n" .
            "> ###### 当前设备GUID：{$params['user']['guid']}\n" .
            "> ###### 当前设备下关联账号（用户UID）：{$uids}\n" .
            "> ###### {$params['datetime']} 预警 \n";

        $this->markdownMsg('充值预警', $text);
    }

    /**
     * text类型
     *
     * @param string $content 消息内容
     * @param array $atMobiles 被@人的手机号(在content里添加@人的手机号)
     * @param bool $isAtAll @所有人时：true，否则为：false
     * @return mixed
     * */
    private function textMsg($content, $atMobiles = [], $isAtAll = false)
    {
        $textString = json_encode([
            'msgtype' => 'text',
            'text' => [
                "content" => $content,// 我就是我, 是不一样的烟火@188xxxx3513
            ],
            'at' => [
                'atMobiles' => $atMobiles,// ['188xxxx3513','188xxxx3512']
                'isAtAll' => $isAtAll,
            ]
        ]);

        return curlRequest($this->webHookApi, $textString, false, ['Content-Type: application/json;charset=utf-8'], 3);
    }

    /**
     * link类型
     * @param string $title 消息标题
     * @param string $text 消息内容。如果太长只会部分展示
     * @param string $messageUrl 点击消息跳转的URL
     * @param string $picUrl 图片URL
     * @return mixed
     * */
    private function linkMsg($title, $text, $messageUrl, $picUrl = '')
    {
        $textString = json_encode([
            "msgtype" => "link",
            "link" => [
                "text" => $text,
                "title" => $title,
                "picUrl" => $picUrl,
                "messageUrl" => $messageUrl,
            ]
        ]);

        return curlRequest($this->webHookApi, $textString, false, ['Content-Type: application/json;charset=utf-8'], 3);
    }

    /**
     * markdown类型
     * @param string $title 首屏会话透出的展示内容
     * @param string $text markdown格式的消息
     * @param array $atMobiles 被@人的手机号(在content里添加@人的手机号)
     * @param bool $isAtAll @所有人时：true，否则为：false
     * @return mixed
     * */
    private function markdownMsg($title, $text, $atMobiles = [], $isAtAll = false)
    {
        // text 示例

        // "#### 杭州天气 @156xxxx8827\n" .
        // "> 9度，西北风1级，空气良89，相对温度73%\n\n" .
        // "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" .
        // "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"

        $textString = json_encode([
            "msgtype" => "markdown",
            "markdown" => [
                "title" => $title,
                "text" => $text,
            ],
            "at" => [
                "atMobiles" => $atMobiles,
                "isAtAll" => $isAtAll
            ]
        ], JSON_UNESCAPED_UNICODE);

        return curlRequest($this->webHookApi, $textString, false, ['Content-Type: application/json;charset=utf-8'], 3);
    }

}